package com.coderfans.redis.impl;/*
package com.coderfans.jedistest;

import com.coderfans.exception.SerializeException;
import com.coderfans.serialize.HessianSerialize;
import com.coderfans.serialize.ISerialize;
import com.google.common.collect.Lists;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import java.util.List;

*/
/**
 * Created by ${fanchunshuai} on 2016-9-30.
 *
 * @version: V1.0
 * @Desc:  基于集群分片式的工具类
 * @Copyright (c) 2016 58到家-版权所有
 *//*

public class RedisShardedCacheClient implements ISetRedis,IListRedis {

    */
/**
     * 声明分片redis连接池
     *//*


    public RedisShardedCacheClient(ShardedJedisPool shardedJedisPool){
        this.shardedJedisPool = shardedJedisPool;
    }

    private  ShardedJedisPool shardedJedisPool;
    private ISerialize serializer = new HessianSerialize();

    @Override
    public <T> long sadd(String field, String key, T value) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            return shardedJedis.sadd(generateKey(field,key),generateValue(value));
        } catch (SerializeException e) {
            e.printStackTrace();
        } finally  {
            shardedJedis.close();
        }
        return 0;
    }

    @Override
    public <T> T spop(String field, String key) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            return (T) shardedJedis.spop(generateKey(field,key));
        }finally {
            shardedJedis.close();
        }
    }

    @Override
    public long scard(String field, String key) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
           return shardedJedis.scard(generateKey(field, key));
        }finally {
            shardedJedis.close();
        }
    }

    @Override
    public <T> long srem(String field, String key, T value) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();

        try {
          return   shardedJedis.srem(generateKey(field,key),generateValue(value));
        } catch (SerializeException e) {
            e.printStackTrace();
        }finally {
            shardedJedis.close();
        }
        return 0;
    }

    @Override
    public <T> boolean sismember(String field, String key, T value) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            shardedJedis.sismember(generateKey(field,key),generateValue(value));

        } catch (SerializeException e) {
            e.printStackTrace();
        }finally {
            shardedJedis.close();

        }
        return false;
    }

    @Override
    public void setSerializer(ISerialize serializer) {
        this.serializer = serializer;
    }

    */
/**
     * 生成实际存储的key
     * @param namespace 命名空间 区分不同的键
     * @param key   bean的 id
     * @return
     *//*

    private byte[] generateKey(String namespace,String key ){
        if(key == null || "".equals(key)){
            return null;
        }
        return (namespace + key).getBytes();
    }


    */
/**
     * 将要存储的对象进行序列化
     * @param value
     * @return
     * @throws SerializeException
     *//*

    private byte[] generateValue(Object value) throws SerializeException {
        return serializer.serialize(value);
    }

    public Object disSerialize(byte [] data, Class<Object> value) throws SerializeException {
        return serializer.disSerialize(value,data);
    }

    */
/**   ------------------redis list 接口操作-------------       **//*


    @Override
    public <T> long lpush(String field, String key, T... value) {
        return 0;
    }

    @Override
    public <T> long rpush(String field, String key, T... value) {
        return 0;
    }

    @Override
    public <T> long lpushx(String field, String key, T value) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
          return   shardedJedis.lpushx(generateKey(field,key),serializer.serialize(value));
        } catch (SerializeException e) {
            e.printStackTrace();
        }finally {
            shardedJedis.close();
        }
        return 0;
    }

    @Override
    public <T> long rpushx(String field, String key, T value) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            return shardedJedis.rpushx(generateKey(field,key),serializer.serialize(value));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            shardedJedis.close();
        }
        return 0;
    }

    @Override
    public <T> T lpop(String field, String key,Class<Object> value) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
           return (T)disSerialize(shardedJedis.lpop(generateKey(field,key)),value);
        } catch (SerializeException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public <T> T blpop(String field, String key, int timeout) {
        return null;
    }

    @Override
    public <T> List<T> blpop(String field, String key,Class<Object> value) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            List<T> list = Lists.newArrayList();
            List<byte[]> list1 = shardedJedis.blpop(generateKey(field, key));
            for (byte[] bytes : list1){
                list.add((T)disSerialize(bytes,value));
            }
            return list;
        } catch (SerializeException e) {
            e.printStackTrace();
        }
        return null;
    }

   */
/* @Override
    public <T> List<T> brpop(String field, String key) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            List<T> list = Lists.newArrayList();
            List<byte[]> byteList = shardedJedis.brpop(generateKey(field, key));
            for (byte[] bytes : byteList){
                list.add((T) disSerialize(bytes));
            }
            return list;
        } catch (SerializeException e) {
            e.printStackTrace();
        }
        return null;
    }*//*


    @Override
    public <T> T brpop(String field, String key, int timeout) {
        //not to  be implements....
        return null;
    }

  */
/*  @Override
    public <T> T rpop(String field, String key) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            return (T)disSerialize(shardedJedis.rpop(generateKey(field, key)));
        } catch (SerializeException e) {
            e.printStackTrace();
        }
        return null;
    }
*//*

    */
/*@Override
    public <T> T lindex(String field, String key, long index) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
           return (T)disSerialize(shardedJedis.lindex(generateKey(field,key),index));
        } catch (SerializeException e) {
            e.printStackTrace();
        }finally {
            shardedJedis.close();
        }
        return null;
    }*//*


    @Override
    public long llen(String field, String key) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            return shardedJedis.llen(generateKey(field,key));
        }finally {
            shardedJedis.close();
        }
    }

    @Override
    public <T> long lrem(String field, String key,long count, T value) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
           return shardedJedis.lrem(generateKey(field,key),count,serializer.serialize(value));
        } catch (SerializeException e) {
            e.printStackTrace();
        }finally {
            shardedJedis.close();
        }
        return 0;
    }

    @Override
    public <T> boolean lset(String field, String key, T value, long index) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
          return "OK".equals(shardedJedis.lset(generateKey(field,key),index,serializer.serialize(value)));
        } catch (SerializeException e) {
            e.printStackTrace();
        }finally {
            shardedJedis.close();
        }
        return false;
    }

    @Override
    public boolean ltrim(String field, String key, long start, long stop) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            return "OK".equals(shardedJedis.ltrim(generateKey(field,key),start,stop));
        }finally {
            shardedJedis.close();
        }
    }

   */
/* @Override
    public <T> List<T> lrange(String field, String key, long start, long stop) {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        try {
            List<T> list = Lists.newArrayList();
            List<byte[]> bytes = shardedJedis.lrange(generateKey(field,key),start,stop);
            for (byte [] bytes1 : bytes){
                list.add((T)disSerialize(bytes1));
            }
            return list;
        } catch (SerializeException e) {
            e.printStackTrace();
        } finally {
            shardedJedis.close();
        }
        return null;
    }*//*

}
*/
